package lab03.Test05;


import java.io.*;

/**
 * Created by Administrator on 2017/12/10.
 */
public class Test05 {
    public static void main(String[] args) {
        String path = "D:\\dev\\shiyan\\mima.txt";
        BufferedReader reader = null;
        BufferedWriter writer = null;
        String origin = "Hello";
        String line;
        String deciphering;
        try {
            writer = new BufferedWriter(new FileWriter(path));
            byte[] bytes = origin.getBytes();
            char[] chars = new char[bytes.length];
            for (int i = 0; i < bytes.length; i++) {
                bytes[i] += 1;
                chars[i] = (char) bytes[i];
                System.out.println("正在加密...");
            }//加密算法
            writer.write(chars);//写文件
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        try {
            reader = new BufferedReader(new FileReader(path));
            while ((line = reader.readLine()) != null) {
                System.out.println("加密之后：" + line);
                byte[] bytes = line.getBytes();
                char[] chars = new char[bytes.length];
                for (int i = 0; i < chars.length; i++) {
                    bytes[i]-=1;
                    chars[i] =(char)bytes[i];
                }
                deciphering =new String(chars);
                System.out.println("解密之后："+deciphering);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
